package simple.form.dao;

import org.apache.log4j.Logger;
import simple.form.controller.SysContent;
import simple.form.utils.SafeConvertUtil;

import java.util.*;

/**
 * 创建sql
 * Created by Administrator on 2017/3/1.
 */
public class MysqlCreator extends SqlCreator{
    public static Logger log4j = Logger.getLogger(MysqlCreator.class);

    /**
     * 创建查询sql
     * @param bean,实体对象
     * @return
     */
    public String createFind(Object bean){
        if(!beanIsTable(bean)){
            return null;
        }

        StringBuilder sql = new StringBuilder();
        StringBuilder sqlFindField = new StringBuilder();
        StringBuilder sqlWhere = new StringBuilder();
        String beanName = bean.getClass().getName();
        Table table = SysContent.beansMappingTables.get(beanName);

        Map<String,Object> sqlFieldMappingValue = getColumnMapping(bean);
        Iterator iter = sqlFieldMappingValue.entrySet().iterator();
        int index=0;
        int size = sqlFieldMappingValue.size();
        List<String> whereParam = new ArrayList<>();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            if(index!=size-1){
                sqlFindField.append(String.format("%s, ", key));
                if(value!=null && !"0".equals(SafeConvertUtil.toString(value))){
                    whereParam.add(String.format("%s='%s'",key,value));
                }

            }else{
                sqlFindField.append(String.format("%s",key));
                if(value!=null && !"0".equals(SafeConvertUtil.toString(value))){
                    whereParam.add(String.format("%s='%s'",key,value));
                }

            }
            ++index;

        }
        for(int i=0,s=whereParam.size();i<s;i++){
            if(i==s-1){
                sqlWhere.append(whereParam.get(i));
            }else {
                sqlWhere.append(whereParam.get(i)+" and ");
            }
        }
        if(whereParam.isEmpty()){
            sql.append(String.format("select %s from %s ",sqlFindField,table.getTableName()));
        }else{
            sql.append(String.format("select %s from %s where %s",sqlFindField,table.getTableName(),sqlWhere));
        }

        return sql.toString();
    }
    /**
     * 创建insert语句
     * @param bean
     * @return
     */
    @Override
    public String createInsert(Object bean){
        if(!beanIsTable(bean)){
            return null;
        }
        StringBuilder sql = new StringBuilder();
        StringBuilder sqlField = new StringBuilder();
        StringBuilder sqlValue = new StringBuilder();
        String beanName = bean.getClass().getName();
        Table table = SysContent.beansMappingTables.get(beanName);
        String tableName = table.getTableName();

        sql.append(String.format("insert into %s ",tableName));
        Map<String,Object> sqlFieldMappingValue = getColumnMapping(bean);
        Iterator iter = sqlFieldMappingValue.entrySet().iterator();
        int index=0;
        int size = sqlFieldMappingValue.size();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            if(value!=null){
                if(index!=size-1){
                    sqlField.append(String.format("%s,",key));
                    sqlValue.append(String.format("'%s',",value));
                }else{
                    sqlField.append(key);
                    sqlValue.append(String.format("'%s'",value));
                }
            }
            index++;
        }
        sql.append(String.format(" (%s) values (%s)",sqlField,sqlValue));
        return sql.toString();

    }

    /**
     * 创建update语句
     * @param bean
     * @return
     */
    @Override
    public String  createUpdateByKey(Object bean){
        if(!beanIsTable(bean)){
            return "";
        }
        StringBuilder sql = new StringBuilder();
        StringBuilder sqlUpdateValue = new StringBuilder();
        String beanName = bean.getClass().getName();
        Table table = SysContent.beansMappingTables.get(beanName);

        sql.append(String.format("update %s set ",table.getTableName()));
        Map<String,Object> sqlFieldMappingValue = getColumnMapping(bean);
        Iterator iter = sqlFieldMappingValue.entrySet().iterator();

        Object keyValue = null;
        List<String> updateValues = new ArrayList<>();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            if(key.toString().equalsIgnoreCase(table.getKey())){
                keyValue = value;
                continue;
            }
            if(value!=null&&!"0".equals(SafeConvertUtil.toString(value))){
                updateValues.add(String.format("%s='%s'",key,value));

            }

        }
        if(keyValue==null){
            return "";
        }
        for(int i=0,size=updateValues.size();i<size;i++){
            if(i==size-1){
                sqlUpdateValue.append(updateValues.get(i));
            }else{
                sqlUpdateValue.append(String.format("%s ,",updateValues.get(i)));
            }
        }

        sql.append(sqlUpdateValue).append(String.format(" where %s='%s'",table.getKey(),keyValue));
        return sql.toString();
    }

    /***
     * 创建删除语句
     * @param bean
     * @return
     */
    @Override
    public String createDelete(Object bean){
        if(!beanIsTable(bean)){
            return null;
        }
        StringBuilder sql = new StringBuilder();
        StringBuilder sqlWhere = new StringBuilder();
        String beanName = bean.getClass().getName();
        Table table = SysContent.beansMappingTables.get(beanName);

        sql.append(String.format("delete from %s where ",table.getTableName()));
        Map<String,Object> sqlFieldMappingValue = getColumnMapping(bean);
        Iterator iter = sqlFieldMappingValue.entrySet().iterator();
        List<String> whereParam = new ArrayList<>();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            if(value!=null && !"0".equals(SafeConvertUtil.toString(value))){
                whereParam.add(String.format("%s='%s'",key,value));
            }


        }
        for(String where:whereParam){
            sqlWhere.append(where);
        }
        sql.append(sqlWhere);
        return sql.toString();
    }
}
